import 'package:flutter/material.dart';
import 'package:my_ebook_app/pages/theme_notifier.dart';
import 'package:provider/provider.dart';
import 'dart:math' as math;
import 'package:my_ebook_app/common/sizes.dart';

class SplashPage extends StatefulWidget {
  const SplashPage({super.key});

  @override
  State<SplashPage> createState() => _SplashPageState();
}

class _SplashPageState extends State<SplashPage> with SingleTickerProviderStateMixin {
  bool _animate = false;

  @override
  void initState() {
    super.initState();
    _loadThemeAndAnimate();
    Future.delayed(Duration(microseconds: 1), () {
      setState(() {
        _animate = true;
      });
    });
  }

  Future<void> _loadThemeAndAnimate() async {
    final themeNotifier = Provider.of<ThemeNotifier>(context, listen: false);
    await themeNotifier.loadTheme(); // 等待主题加载完成

    setState(() {
    });

    Future.delayed(Duration(milliseconds: 1), () {
      if (mounted) {
        setState(() {
          _animate = true;
        });
      }
    });
  }
  @override
  Widget build(BuildContext context) {
    final isDarkMode = Theme.of(context).brightness == Brightness.dark;
    return Scaffold(
      body: Stack(
        children: [
          // 上方左侧的书
          AnimatedPositioned(
            duration: Duration(milliseconds: 700),
            curve: Curves.easeInOut,
            top: _animate ? 20 : -400,
            left: _animate ? -150 : -550,
            child: ConstrainedBox(
                constraints: BoxConstraints(
                  maxWidth: 300,
                  maxHeight: 400,
                ),
                child: FractionallySizedBox(
                    widthFactor: 0.75,
                    heightFactor: 0.75,
                    child: Transform.rotate(
                      angle: math.pi / 3,
                      child: isDarkMode
                          ? ShaderMask(
                        shaderCallback: (Rect bounds) {
                          return LinearGradient(
                            colors: [Color(0x80000000), Color(0x80000000)],
                          ).createShader(bounds);
                        },
                        blendMode: BlendMode.darken,
                        child: Container(
                          decoration: BoxDecoration(
                            image: DecorationImage(
                              image: AssetImage('lib/common/images/splashBook1.jpg'),
                              fit: BoxFit.cover,
                            ),
                            boxShadow: [
                              BoxShadow(
                                color: Colors.black12,
                                blurRadius: 5,
                                spreadRadius: 2,
                              ),
                            ],
                          ),
                        ),
                      )
                          : Container(
                        decoration: BoxDecoration(
                          image: DecorationImage(
                            image: AssetImage('lib/common/images/splashBook1.jpg'),
                            fit: BoxFit.cover,
                          ),
                          boxShadow: [
                            BoxShadow(
                              color: Colors.black12,
                              blurRadius: 5,
                              spreadRadius: 2,
                            ),
                          ],
                        ),
                      ),
                    )
                )
            ),
          ),
          // 上方右侧的书
          AnimatedPositioned(
            duration: Duration(milliseconds: 800),
            curve: Curves.easeInOut,
            top: _animate ? -180 : -580,
            right: _animate ? -50 : -450,
            child: ConstrainedBox(
                constraints: BoxConstraints(
                  maxWidth: 300, // 设置最大宽度
                  maxHeight: 400, // 设置最大高度
                ),
                child: FractionallySizedBox(
                  widthFactor: 0.75,
                  heightFactor: 0.75,
                  child: Transform.rotate(
                    angle: -math.pi*8 / 9,
                    child: isDarkMode
                        ? ShaderMask(
                      shaderCallback: (Rect bounds) {
                        return LinearGradient(
                          colors: [Color(0x80000000), Color(0x80000000)],
                        ).createShader(bounds);
                      },
                      blendMode: BlendMode.darken,
                      child: Container(
                        decoration: BoxDecoration(
                          image: DecorationImage(
                            image: AssetImage('lib/common/images/splashBook2.jpg'),
                            fit: BoxFit.cover,
                          ),
                          boxShadow: [
                            BoxShadow(
                              color: Colors.black12,
                              blurRadius: 5,
                              spreadRadius: 2,
                            ),
                          ],
                        ),
                      ),
                    )
                        : Container(
                      decoration: BoxDecoration(
                        image: DecorationImage(
                          image: AssetImage('lib/common/images/splashBook2.jpg'),
                          fit: BoxFit.cover,
                        ),
                        boxShadow: [
                          BoxShadow(
                            color: Colors.black12,
                            blurRadius: 5,
                            spreadRadius: 2,
                          ),
                        ],
                      ),
                    ),
                  ),
                )
            ),
          ),
          // 左下角的书
          AnimatedPositioned(
            duration: Duration(milliseconds: 900),
            curve: Curves.easeInOut,
            bottom: _animate ? 20 : -380,
            left: _animate ? -120 : -520,
            child: ConstrainedBox(
                constraints: BoxConstraints(
                  maxWidth: 300, // 设置最大宽度
                  maxHeight: 400, // 设置最大高度
                ),
                child: FractionallySizedBox(
                    widthFactor: 0.75,
                    heightFactor: 0.75,
                    child: Transform.rotate(
                      angle: -math.pi / 6,
                      child: Container(
                        decoration: BoxDecoration(
                          image: DecorationImage(
                            image: AssetImage('lib/common/images/splashBook3.jpg'),
                            fit: BoxFit.cover,
                            colorFilter: isDarkMode?
                            ColorFilter.mode(
                              Color(0x80000000),
                              BlendMode.darken,
                            )
                                :null,
                          ),
                          boxShadow: [
                            BoxShadow(
                              color: Colors.black38,
                              blurRadius: 3,
                              spreadRadius: 2,
                            ),
                          ],
                        ),
                      ),
                    )
                )
            ),
          ),
          // 右下角的书
          AnimatedPositioned(
            duration: Duration(seconds: 1),
            curve: Curves.easeInOut,
            bottom: _animate ? 150 : -250,
            right: _animate ? -178 : -578,
            child: ConstrainedBox(
                constraints: BoxConstraints(
                  maxWidth: 300, // 设置最大宽度
                  maxHeight: 400, // 设置最大高度
                ),
                child: FractionallySizedBox(
                  widthFactor: 0.75,
                  heightFactor: 0.75,
                  child: Container(
                    decoration: BoxDecoration(
                      image: DecorationImage(
                        image: AssetImage('lib/common/images/splashBook4.jpg'),
                        fit: BoxFit.cover,
                        colorFilter: isDarkMode?
                        ColorFilter.mode(
                          Color(0x80000000),
                          BlendMode.darken,
                        )
                            :null,
                      ),
                      boxShadow: [
                        BoxShadow(
                          color: Colors.black54,
                          blurRadius: 3,
                          spreadRadius: 2,
                        ),
                      ],
                    ),
                  ),
                )
            ),
          ),
          // 中间标题
          Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Text.rich(
                  TextSpan(
                    style: TextStyle(
                      fontSize: 50,
                      fontWeight: FontWeight.bold,
                      color: isDarkMode?Colors.black45:Colors.black,
                    ),
                    children: <TextSpan>[
                      TextSpan(text: 'My Ebook\n'),
                      TextSpan(text: 'Reader'),
                    ],
                  ),
                  textAlign: TextAlign.center,
                ),
              ],
            ),
          ),
          // 版权信息
          Positioned(
            bottom: 40,
            left: 0,
            right: 0,
            child: Column(
              children: [
                Text('FROM',
                  style: TextStyle(
                    fontSize: copyrightFontSize0,
                  ) ,
                ),
                SizedBox(height: 5),
                Text("WWY",
                  style: TextStyle(
                    fontSize: copyrightFontSize0,
                  ) ,
                ),
                SizedBox(height: 25),
                Text("My Ebook Reader. All Rights Reserved.",
                  style: TextStyle(
                    fontSize: copyrightFontSize,
                    color: Colors.grey
                  ) ,
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}

